package com.shuoyue.ycgk.ui.news;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.News;
import com.shuoyue.ycgk.entity.NewsType;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.news.contract.NewsContract;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

import butterknife.BindView;

public class FragmentNewsItem extends BaseMvpFragment<NewsContract.Presenter> implements NewsContract.View {


    Type baseType;
    public NewsType topType;
    String title;
    @BindView(R.id.newsRecycleView)
    RecyclerView newsRecycleView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    ListWithPage listWithPage;
    NewsAdapter adapter;

    @Override
    public void initParams(Bundle bundle) {
        baseType = (Type) bundle.getSerializable("baseType");
        topType = (NewsType) bundle.getSerializable("topType");

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_news_item;
    }

    @Override
    protected void initView() {
        newsRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        newsRecycleView.addItemDecoration(new SpacesItemDecoration(1, false));
        refreshlayout.setEnableLoadMore(false);
        adapter = new NewsAdapter(null);
        newsRecycleView.setAdapter(adapter);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            if (topType != null)
                mPresenter.getNewsList(baseType.getId(),
                        1,
                        topType.getId(),
                        null);
            refreshLayout.finishRefresh(2000);
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            if (topType != null)
                mPresenter.getNewsList(baseType.getId(),
                        listWithPage.getCurrent() + 1,
                        topType.getId(),
                        null);
            refreshLayout.finishLoadMore(2000);
        });
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            News news = (News) adapter.getItem(position);
//            startActivity(new Intent(mContext, NewsInfoActivity.class)
//                    .putExtra("id", news.getId()));
//            startActivity(new Intent(mContext, NewsInfoWebActivity.class)
//                    .putExtra("id", news.getId()));
            startActivity(new Intent(mContext, NewsInfoFullWebActivity.class)
                    .putExtra("id", news.getId()));
        });

        registEmptyView(adapter);
    }

    @Override
    protected void initData() {
        mPresenter = new NewsContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getNewsList(baseType.getId(), 1, topType.getId(), null);
    }

    public static FragmentNewsItem getInstance(Type baseType,
                                               NewsType topType) {
        FragmentNewsItem fragment = new FragmentNewsItem();
        fragment.title = topType.getType();
        Bundle bundle = new Bundle();
        bundle.putSerializable("baseType", baseType);
        bundle.putSerializable("topType", topType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setNewsInfo(News newsInfo) {

    }

    @Override
    public void setNewsList(ListWithPage<News> list) {
        listWithPage = list;
        if (list.getCurrent() == 1) {
            adapter.resetData(list.getRecords());
        } else {
            adapter.addData(list.getRecords());
        }
        refreshlayout.setEnableLoadMore(list.getCurrent() != list.getPages()&& list.getPages() > 0);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void setNewsTypeList(List<NewsType> newsTypeList) {

    }
}
